﻿using AutoTest.Common.EnumExt;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;

namespace AutoTest.Common.UtilsHelper
{
    /// <summary>
    /// 枚举的帮助类
    /// </summary>
    public static class EnumHelper
    {
        /// <summary>
        /// 遍历获取枚举的值
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static List<EnumItem> GetEnumList(Type type)
        {
            var fields = type.GetRuntimeFields();
            var list = new List<EnumItem>();
            foreach (var field in fields)
            {
                string description;
                var attribute = field.GetCustomAttribute<DescriptionAttribute>(false);
                var ignoreAttribute = field.GetCustomAttribute<EnumIgnoreAttribute>(false)!;
                if (attribute is not null
                    && ignoreAttribute is not null
                    && !ignoreAttribute.CastTo<EnumIgnoreAttribute>().IsIgnore
                    )
                {
                    description = attribute.Description;
                    var objValue = field.GetValue(null)!;
                    string v = objValue.CastNoSafe<int>().ToString();
                    var item = new EnumItem()
                    {
                        Description = description,
                        IValue = EasyTool.ConvertUtil.ToInt32(v),
                        Value = field.GetValue(null)!.CastTo<Enum>(),
                        ValueString = objValue?.ToString()??""
                    };
                    list.Add(item);
                }
            }
            return list;

        }

        /// <summary>
        /// 遍历获取枚举的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<EnumItem> GetEnumList<T>()
        {
            Type type = typeof(T);
            return GetEnumList(type);
        }

        /// <summary>
        /// 字符串转成枚举类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumstring"></param>
        /// <param name="defaultValue"></param>
        /// <param name="ignorecase"></param>
        /// <returns></returns>
        public static T ToEnum<T>(string enumstring, T defaultValue = default, bool ignorecase = true) where T : struct
        {
            T e = defaultValue;
            try
            {
                e = (T)Enum.Parse(typeof(T), enumstring, ignorecase);
            }
            catch (Exception)
            { }

            return e;
        }

        /// <summary>
        /// 字符串转枚举
        /// </summary>
        /// <param name="enumstring"></param>
        /// <param name="enumtype"></param>
        /// <param name="defaultValue"></param>
        /// <param name="ignorecase"></param>
        /// <returns></returns>
        public static object ToEnum(string enumstring,Type enumtype, object defaultValue, bool ignorecase=true)
        {
            object v = defaultValue;
            try
            {
                v = Enum.Parse(enumtype, enumstring, ignorecase);
            }
            catch (Exception)
            { }

            return v;
        }


        /// <summary>
        /// 获取枚举的名字
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumValue"></param>
        /// <returns></returns>
        public static string GetEnumName<T>(T enumValue) where T: Enum
        {
            return Enum.GetName(typeof(T), enumValue)??"";
        }

        /// <summary>
        /// 枚举转字符串
        /// </summary>
        /// <param name="enumType"></param>
        /// <param name="enumValue"></param>
        /// <returns></returns>
        public static string GetEnumName(Type enumType,object enumValue)
        {
            return Enum.GetName(enumType, enumValue)??"";
        }



    }
}
